package com.gin.supermarket_inventory_system.inventory.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gin.database.service.MyService;
import com.gin.supermarket_inventory_system.inventory.entity.InventoryReceiptPo;

import java.util.List;

/**
 *
 * @since : 2023/10/11 11:43
 * @author : ginstone
 * @version : v1.0.0
 **/
public interface InventoryReceiptService extends MyService<InventoryReceiptPo> {

    /**
     * @param start 时间区间，起
     * @param end 时间区间，止
     * @return 统计指定时间区间, 按类型分组的单据数量，总额
     */
    default List<InventoryReceiptPo> countGroupByType(long start, long end) {
        final QueryWrapper<InventoryReceiptPo> qw = new QueryWrapper<>();
        // 查询类型 日期 ，数量，总额
        qw.select("type"
                        , "DATE_FORMAT(FROM_UNIXTIME(time_create), '%Y-%m-%d') as date"
                        , "count(1) as count"
                        , "sum(total_price) as total_price"
                )
                .ge("time_create", start)
                .le("time_create", end)
                .groupBy("type", "date");
        return list(qw);
    }

    /**
     * 查询指定供应商的库存单据数量
     * @param purveyorId 供应商id
     * @return 数量
     */
    default long countByPurveyorId(long purveyorId) {
        final QueryWrapper<InventoryReceiptPo> qw = new QueryWrapper<>();
        qw.eq("purveyor_id", purveyorId);
        return count(qw);
    }
}